package com.electric.qrapp.ui.file.newfile

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.electric.qrapp.entity.bean.FilePreviewModel
import com.electric.qrapp.ui.base.vm.BaseViewModel
import com.electric.qrapp.utils.view.LoadingUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class NewFileViewModel @Inject constructor(
    private val repository: NewFileRepository
) : BaseViewModel() {
    val vmPreview = MutableLiveData<FilePreviewModel>()  //预览页面

    //简易QQ浏览器错误回调
    private val qbErrorList = listOf<String>("filepath error", "TbsReaderDialogClosed", "fileReaderClosed", "openFileReader open in QB", "open QB")

    private val loadingUtils: LoadingUtils by lazy {
        LoadingUtils()
    }

    /**
     *下载文件
     * @param url
     */
    fun getFile(context: Context, url: String) {
        //用户重复点击
        if (loadingLiveDate.value === true) {
            return;
        }

        launch(needLoading = true) {
            repository.getFile(viewModelScope, url, vmPreview)
        }
    }

    fun isCloseFileRead(errMsg: String): Boolean {
        return qbErrorList.contains(errMsg)
    }

    fun loading(context: Context, isShow: Boolean) {
        loadingUtils.showLoading(context, isShow)
    }
}